var $cname;
var $shopId;
var $sStatus2;

$(function() {
	
	//时间插件
	laydate({
		elem: '#autionStartTime', //目标元素。
		format: 'YYYY-MM-DD hh:mm:ss'
	});
	laydate({
		elem: '#autionStopTime', //目标元素。
		format: 'YYYY-MM-DD hh:mm:ss'
	});
	
	
	// 租赁时间弹窗隐藏
	$("#zlTime").hide();
	$("#astartTime").hide();
	$("#astopTime").hide();
		
	$("#zl").click(function(){
		$("#zlTime").show();
		$("#astartTime").hide();
		$("#astopTime").hide();
	});
	
	$("#cs").click(function(){
		$("#zlTime").hide();
		$("#astartTime").hide();
		$("#astopTime").hide();
	});
	
	$("#jp").click(function(){
		$("#zlTime").hide();
		$("#astartTime").show();
		$("#astopTime").show();
	});
	
	
	
	$("#zlTime2").hide();
	$("#astartTime2").hide();
	$("#astopTime2").hide();
	
	$("#zl2").click(function(){
		$("#zlTime2").show();
		$("#astartTime2").hide();
		$("#astopTime2").hide();
	});
	
	$("#cs2").click(function(){
		$("#zlTime2").hide();
		$("#astartTime2").hide();
		$("#astopTime2").hide();
	});
	
	$("#jp2").click(function(){
		$("#zlTime2").hide();
		$("#astartTime2").show();
		$("#astopTime2").show();
	});

/*	$.post("shopSellInfoList", function(data) {
//		 alert(data.data[0].image_url);
		for (var i = 0; i < data.data.length; i++) {
			var a = data.data[i].image_url;
//			alert(a);
			var str = a.split(";");
			var value = str[0];
//			alert(value);
			var img_r=$(".img-responsive")
//			$(img_r[i]).attr("src","http://127.0.0.1:8088/adicms_image"+value);
		  	$(img_r[i]).attr("src","http://192.168.3.37:8088/adicms_image"+value);
		}
	}, "json");
	*/
	
	//添加艺术品信息
	
	// 校验弹框输入数据
	$("#addForm").validate({
		errorElement : 'span',
		errorPlacement : function(error, element) {
			error.appendTo(element.parent());
		},
		rules : {
			shopName : "required",
			shopPrice : "required",
			shopCount : "required",
			shopDescribe : "required",
			xCoo : "required",
			yCoo : "required",
			shopTypeId : "required"
		},
		msgs : {
			shopName : "请输入艺术品名称",
			shopPrice : "请输入艺术品价格",
			shopCount : "请输入艺术品数量",
			shopDescribe : "请输入艺术品描述",
			xCoo : "请输入经度",
			yCoo : "请输入纬度",
			shopTypeId : "请选择艺术品类型",
		},
		submitHandler : function(form) {
			$('#saveBtn').attr("disabled",true);
		    setTimeout("$('#saveBtn').removeAttr('disabled')",3000);
/*			var url = "addShopSellInfo";
			var shopName = $("#shopName").val();
			var shopPrice = $("#shopPrice").val();
			var shopCount = $("#shopCount").val();
			var shopDescribe = $("#shopDescribe").val();
			var xCoo = $("#xCoo").val();
			var yCoo = $("#yCoo").val();
			var shopTypeId = $("#shopTypeId").val();*/
//			var file = $("#file").val();
			
		
			
			 var form = new FormData(document.getElementById("addForm"));
			
			 $.ajax({
	                url:"addShopSellInfos",
	                type:"post",
	                dataType:"json",
	                data:form,
	                processData:false,
	                contentType:false,
	                success:function(data){
	                	if(data.code == 200){
	                		setTimeout(function(){
	    						layer.msg("添加成功", {icon : 1,time : 1500});
	    						$(form).resetForm();
	    						layer.closeAll('loading');
	    						$("#modal-add").modal('hide');
	    						$("#table").bootstrapTable('refresh');
	    					},1500);
	                	}
	                },
	                error:function(e){
	                	layer.msg("添加失败", {icon : 2,time : 1500,});
						layer.closeAll('loading');
	                }
	            });       
			
			layer.load(2);
		},
	});
	
	
	//修改 查看
	$("#edit").click(function() {
		var ids = getSelections(); 
		var shopInfoList = getSelectionss(); 
		if (ids.length == 1) {
			// 根据id 查询信息 
			$("input[name=shopId]").val(shopInfoList[0].shop_id);
			$("input[name=shopName2]").val(shopInfoList[0].shop_name);
			$("input[name=shopPrice2]").val(shopInfoList[0].shop_price);
			$("input[name=shopCount2]").val(shopInfoList[0].shop_count);
			$("textarea[name=shopDescribe2]").val(shopInfoList[0].shop_describe);
			$("select[name=shopTypeId2]").val(shopInfoList[0].shop_type_id);
			$("input[name=years2]").val(shopInfoList[0].years);
			$("input[name=author2]").val(shopInfoList[0].author);
			$("input[name=standard2]").val(shopInfoList[0].standard);
			$("input[name=shopZltime2]").val(shopInfoList[0].shop_zltime);
			$("input[name=autionStartTime2]").val(shopInfoList[0].aution_start_time);
			$("input[name=autionStopTime2]").val(shopInfoList[0].aution_stop_time);
//			$("input[name=sStatus2]").val(shopInfoList[0].s_status);
			if(shopInfoList[0].s_status == 0){
				$("#cs2").attr("checked",true);
			}else if(shopInfoList[0].s_status == 1){
				$("#zl2").attr("checked",true);
				$("#zlTime2").show();
				$("#astartTime2").hide();
				$("#astopTime2").hide();
			}else{
				$("#jp2").attr("checked",true);
				$("#zlTime2").hide();
				$("#astartTime2").show();
				$("#astopTime2").show();
			}
			$('#modal-edit').modal('toggle');

		}else if(ids.length == 0){
			layer.msg("未选择数据");
		}else{
			layer.msg("请勿勾选多个");
		}
	});
	
	
	$("#zl2").click(function(){
	    sStatus2 = 1;
	});
	
	$("#cs2").click(function(){
		sStatus2 = 0;
	});
	
	$("#jp2").click(function(){
		sStatus2 = 2;
	});
	
	//确认修改
	$("#editBtn").click(function(){
			var shopId = $("#shopId").val();
			var shopName2 = $("#shopName2").val();
			var shopPrice2 = $("#shopPrice2").val();
			var shopCount2 = $("#shopCount2").val();
			var shopDescribe2 = $("#shopDescribe2").val();
			var shopTypeId2 = $("#shopTypeId2").val();
			var years2 = $("#years2").val();
			var author2 = $("#author2").val();
			var standard2 = $("#standard2").val();
			var shopZltime2 = $("#shopZltime2").val();

			var sStatus2 =  $("input[name='sStatus2']:checked").val();
//			var a = $('input:radio:checked').val();
//			var v = $("input[type='radio']:checked").val();
			
			if((sStatus2 ==1 && $("#shopZltime2").val() == null) ||(sStatus2 ==1 && $("#shopZltime2").val() == "")){
				layer.msg("租赁时间不允许为空");
				return;
			}
			$.post("updateShopSellInfoById",{
				shopId : shopId,
				shopName : shopName2,
				shopPrice : shopPrice2,
				shopCount : shopCount2,
				shopDescribe : shopDescribe2,
				shopTypeId : shopTypeId2,
				years : years2,
				author : author2,
				standard : standard2,
				shopZltime : shopZltime2,
				sStatus : sStatus2
			},function(data){
				if(data.code == 200){
					layer.msg(data.msg, {icon : 1,time : 1500,});
				}else{
					layer.msg(data.msg, {icon : 2,time : 1500,});
				}
			},"json");
	});
	
	
	
	// 批量删除
	$("#deleteBtn").click(function() {
		var ids = getSelections();
		if (ids == "") {
			layer.msg("未选择数据");
		} else {
			layer.confirm('确定要删除所选数据吗？',{btn: ['确定','取消']},function (){
				$.post("deleteShop", {
					pIds : JSON.stringify(ids)
				}, function(data) {
					if(data.code == 200){
						layer.msg(data.msg, {icon : 1,time : 1500,});
					}else{
						layer.msg(data.msg, {icon : 2,time : 1500,});
					}
					$('#table').bootstrapTable('remove', {
						field : 'shop_id',
						values : ids,
					});
				}, "json");
				return true;
			});
		}
	});
	
	/*得到选中的行*/
	function getSelections() {
		return $.map($('#table').bootstrapTable('getSelections'),function(row) {
			return row.shop_id;
		});
	}
	
	function getSelectionss() {
		return $.map($('#table').bootstrapTable('getSelections'),function(row) {
			return row;
		});
	}
	
//   $("#all").click(function(){
//        // this 全选的复选框
//        var status=this.checked;
//        //获取name=box的复选框 遍历输出复选框
//        $("input[name=checked]").each(function(){
//            this.checked=status;
//        });
//    });
   
//   function getSelections() {
//	    var cks=document.getElementsByName("checked");
//	    var str="";
//	   var data = new Array();
//		 for(var i=0;i<cks.length;i++){
//			 if(cks[i].checked){
//				 str = cks[i].value;
//				 data[i] = str; 
//			 }
//		 }
//		 return data;
//   }
   
});

$(":radio").click(function(){
	$("#table").bootstrapTable('refresh');
});

$("#standDown").change(function(){
	$("#table").bootstrapTable('refresh');
});

function queryParams(p){
	var status = $('input:radio[name="status"]:checked').val();
	var standDown = $('#standDown option:selected').val();
	p.status = status;
	p.standDown = standDown;
	return p;
}

/*
 * 格式化操作列
 */
function state(value, row, index) {
	var image_url = row.image_url;
//	var image = image_url.substr(0, 25);
	
	var strs= new Array(); //定义一数组 
	strs=image_url.split(";"); //字符分割 
	var image;
	for (i=0;i<strs.length-1 ;i++ ) 
	{ 
		image = strs[i];
		break;
	} 
	if(image_url != null){
		image;
		return [
//			"<img src=" + "http://192.168.3.37:8088/adicms_image" + image + " style='width:150px;height:150px;' class='img-rounded'/>"
			"<img src=" + "http://47.104.67.69:8088/adicms_image" + image + " style='width:150px;height:150px;' class='img-rounded'/>"
// 			"<img src=" + "http://127.0.0.1:8088/adicms_image" + image + " style='width:150px;height:150px;' class='img-rounded'/>"
		].join('');	
	}
}

function operating_status(value, row, index) {
	var stand_down = row.stand_down;
	if(stand_down == 0){
		return [
			"<a class='lower' href='javascript:void(0)'>下架</a>"
		].join('');	
	}
	if(stand_down == 1){
		return [
			"<a class='onself' href='javascript:void(0)'>上架</a>"
		].join('');	
	}
}


/**
 * 操作列的事件监听
 */
window.operateEvents = {
	'click .lower': function(e, value, row, index) {
		var shopId = row.shop_id;
		$.post("updateShopStatus",{
			shopId : shopId,
			standDown : 1
		},function(data){
			if(data.code == 200){
				layer.msg(data.msg, {icon : 1,time : 1500,});
			}else{
				layer.msg(data.msg, {icon : 2,time : 1500,});
			}
			$("#table").bootstrapTable('refresh');
		},"json");
	},
	'click .onself': function(e, value, row, index) {
		var shopId = row.shop_id;
		$.post("updateShopStatus",{
			shopId : shopId,
			standDown : 0
		},function(data){
			if(data.code == 200){
				layer.msg(data.msg, {icon : 1,time : 1500,});
			}else{
				layer.msg(data.msg, {icon : 2,time : 1500,});
			}
			$("#table").bootstrapTable('refresh');
		},"json");
	}
};


